//
//  CNBBSForumListViewModel.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/5/30.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSForumListViewModel.h"

@implementation CNBBSForumListViewModel

/**
 *  获取有多少个
 *
 *  @return 返回个数
 */
-(NSInteger)rowNumber
{
    return self.dataArr.count;
}

- (void)getDataFromNetCompleteHandle:(CompletionHandle)completionHandle{
    
    self.dataTask = [CNBBSNetManager getBBSForumListCompletionHandle:^(CNForumListModel *model, NSError *error) {
    
        if (!error) {
            self.code = model.code;
            self.message = model.msg;
            if (self.code == 200)
            {
                [self.dataArr removeAllObjects];
                [self.dataArr addObjectsFromArray:model.data];
            }
        }
        completionHandle(error);
    }];
}

/**
 *  获取CNForumListDataModel
 *
 *  @param row 根据行
 *
 *  @return 返回CNForumListDataModel
 */

-(CNForumListDataModel *)forumListDataModelForRowInForumList:(NSInteger)row
{
    if (row == 0) {
        return self.dataArr.firstObject;
    }
    return self.dataArr[row];
}

-(CNForumListDataChildForumsModel *)childListDataModelForRowInForumList:(NSInteger)row AndSection:(NSInteger)section
{
    if (row == 0) {
        return [self forumListDataModelForRowInForumList:section].child_forums.firstObject;
    }
    return [self forumListDataModelForRowInForumList:section].child_forums[row];
}

/** 大版块是否关注  */
-(BOOL)isFavoritedForRow:(NSInteger)row
{
    return [self forumListDataModelForRowInForumList:row].is_favorited;
}

-(void)setIsFavorited:(BOOL)isFavorited InForRow:(NSInteger)row
{
    [[self forumListDataModelForRowInForumList:row] setIs_favorited:isFavorited];
}

/** 大版块类型  */
-(NSString *)urlnameForRow:(NSInteger)row
{
    return [self forumListDataModelForRowInForumList:row].urlname;
}

/** 大版块名称  */
-(NSString *)nameForRow:(NSInteger)row
{
    return [self forumListDataModelForRowInForumList:row].name;
}

/** 大版块是否是技术贴  */
-(BOOL)techForumForRow:(NSInteger)row
{
    return [self forumListDataModelForRowInForumList:row].tech_forum;
}

/** 大版块对应小版块  */
-(NSArray *)childForumsForRow:(NSInteger)row
{
    return [self forumListDataModelForRowInForumList:row].child_forums;
}

/** 小版块个数  */
-(NSInteger)childRowNumberForRow:(NSInteger)row
{
    return [self forumListDataModelForRowInForumList:row].child_forums.count;
}
/** 小版块是否关注  */
-(BOOL)isFavoritedForRow:(NSInteger)row AndSection:(NSInteger)section
{
    return [self childListDataModelForRowInForumList:row AndSection:section].is_favorited;
}

-(void)setIsFavorited:(BOOL)isFavorited InForRow:(NSInteger)row AndSection:(NSInteger)section
{
    [[self childListDataModelForRowInForumList:row AndSection:section] setIs_favorited:isFavorited];
}

/** 小版块类型  */
-(NSString *)urlnameForRow:(NSInteger)row AndSection:(NSInteger)section
{
    return [self childListDataModelForRowInForumList:row AndSection:section].urlname;
}

/** 小版块名称  */
-(NSString *)nameForRow:(NSInteger)row AndSection:(NSInteger)section
{
    return [self childListDataModelForRowInForumList:row AndSection:section].name;
}

/** 小版块是否是技术贴  */
-(BOOL)techForumForRow:(NSInteger)row AndSection:(NSInteger)section
{
    return [self childListDataModelForRowInForumList:row AndSection:section].tech_forum;
}

-(NSArray *)isFavoritedNameArray
{
    NSMutableArray *mutArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < self.rowNumber; i++)
    {
        if ([self isFavoritedForRow:i])
        {
            [mutArray addObject:[self nameForRow:i]];
        }
        for (int j = 0; j < [self childRowNumberForRow:i]; j++)
        {
            if ([self isFavoritedForRow:j AndSection:i])
            {
                [mutArray addObject:[self nameForRow:j AndSection:i]];
            }
        }
    }
    return [mutArray copy];
}


-(NSArray *)isFavoritedTechForumArray
{
    NSMutableArray *mutArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < self.rowNumber; i++)
    {
        if ([self isFavoritedForRow:i])
        {
            [mutArray addObject:@([self techForumForRow:i])];
        }
        for (int j = 0; j < [self childRowNumberForRow:i]; j++)
        {
            if ([self isFavoritedForRow:j AndSection:i])
            {
                [mutArray addObject:@([self techForumForRow:j AndSection:i])];
            }
        }
    }
    
    
    return [mutArray copy];
}

-(NSArray *)isFavoritedUrlnameArray
{
    NSMutableArray *mutArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < self.rowNumber; i++)
    {
        if ([self isFavoritedForRow:i])
        {
            [mutArray addObject:[self urlnameForRow:i]];
        }
        for (int j = 0; j < [self childRowNumberForRow:i]; j++)
        {
            if ([self isFavoritedForRow:j AndSection:i])
            {
                [mutArray addObject:[self urlnameForRow:j AndSection:i]];
            }
        }
    }

    return [mutArray copy];
}


/**
论坛任务数
*/
-(void)getBBSTaskCountCompletionHandle:(CompletionHandle)completionHandle
{
    self.dataTask = [CNBBSNetManager getBBSTaskCountCompletionHandle:^(CNBBSPostedModel *model, NSError *error) {
   
        if (!error) {
            self.code = model.code;
            self.message = model.msg;
            if (self.code == 200)
            {
                self.taskCount = model.data.count;
            }
        }
        completionHandle(error);
    }];
}
@end
